home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Beziers y otros splines / BezierManual / BezierManual.cs next >
Encoding:
Text File  |  2002-05-08  |  1.5 KB  |  47 lines

  1. //-------------------------------------------
  2. // BezierManual.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class BezierManual: Bezier
  9. {
  10.      public new static void Main()
  11.      {
  12.           Application.Run(new BezierManual());
  13.      }
  14.      public BezierManual()
  15.      {
  16.           Text = "Curva de BΘzier dibujada \"manualmente\"";
  17.      }
  18.      protected override void OnPaint(PaintEventArgs pea)
  19.      {
  20.           base.OnPaint(pea);
  21.  
  22.           BezierSpline(pea.Graphics, Pens.Red, apt);
  23.      }
  24.      void BezierSpline(Graphics grfx, Pen pen, Point[] aptDefine)
  25.      {
  26.           Point[] apt = new Point[100];
  27.  
  28.           for (int i = 0; i < apt.Length; i++)
  29.           {
  30.                float t = (float) i / (apt.Length - 1);
  31.  
  32.                float x = (1 - t) * (1 - t) * (1 - t) * aptDefine[0].X +
  33.                           3 * t  * (1 - t) * (1 - t) * aptDefine[1].X +
  34.                           3 * t * t        * (1 - t) * aptDefine[2].X +
  35.                           t * t * t                  * aptDefine[3].X;
  36.  
  37.                float y = (1 - t) * (1 - t) * (1 - t) * aptDefine[0].Y +
  38.                           3 * t  * (1 - t) * (1 - t) * aptDefine[1].Y +
  39.                           3 * t * t        * (1 - t) * aptDefine[2].Y +
  40.                           t * t * t                  * aptDefine[3].Y;
  41.  
  42.                apt[i] = new Point((int) Math.Round(x), (int) Math.Round(y));
  43.           }
  44.           grfx.DrawLines(pen, apt);
  45.      }
  46. }
  47.